<?php


namespace App\Models;

use Illuminate\Support\Facades\Route;

class Menu
{

    public static function isParentActive($builder, $item, $level = 1)
    {
        return self::checkActive($builder, $item);
    }

    public static function isActive($builder, $item, $level = 1)
    {
        if (empty($item['children'])) {
            return self::checkActive($builder, $item);
        } else {
            foreach ($item['children'] as $v) {
                if (self::checkActive($builder, $v)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static function checkActive($builder, $item)
    {
        $actions=explode('\\', Route::current()->getActionName());
        // $modelName=$actions[count($actions)-2]=='Controllers'?null:$actions[count($actions)-2];
        $func=explode('@', $actions[count($actions)-1]);
        $controllerName=$func[0];  //当前控制器
        $actionName=$func[1];   //当前方法
        $maps = [];
        if (strpos($item['uri'], 'rooms') !== false) {
            $maps = [
                'only_controller' => ['RoomController'],
                'except_action' => []
            ];
        }
        if (preg_match('/\/orders$/', $item['uri'])) {
            $maps = [
                'only_controller' => ['OrderController'],
                'except_action' => []
            ];
        }
        if (preg_match('/\/order_chart$/', $item['uri'])) {
            $maps = [
                'only_controller' => ['OrderChartController'],
                'except_action' => []
            ];
        }


        if ($controllerName == 'StudentController') {
            // if ($actionName == 'edit') {
            //     $student_id = Route::getCurrentRoute()->parameter('student', null);
            //     if ($student_id) {
            //         $student = Student::find($student_id);
            //         if ($student->is_by == Config::STUDENT_BY && preg_match('/student_graduate$/', $item['uri'])) {
            //             return true;
            //         }
            //         if ($student->is_by == Config::STUDENT_BY && preg_match('/\/student$/', $item['uri'])) {
            //             return false;
            //         }
            //     }
            // }
        }

        if (isset($maps['only_controller']) && $maps['only_controller'] && in_array($controllerName, $maps['only_controller'])) {
            // 排除某些action，如果有
            if (isset($maps['except_action']) && $maps['except_action']) {
                // todo
            }
            return true;
        } else {
            return $builder->isActive($item);
        }
        return false;
    }


}